package com.app.repository;

import com.app.configs.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Hendra on 1/6/2015.
 */
public class REmail extends Repository {

    private static final Logger logger = LoggerFactory.getLogger(REmail.class);

    public REmail() {
        super("REmailInbox");
    }

    @Override
    public void post(String params) {
        try {
            url = new URL(uri);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; charset=utf8");
            if (params != null) {
                os = connection.getOutputStream();
                os.write(params.getBytes("UTF-8"));
                os.flush();
            }
            if (connection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : " + connection.getResponseCode() + "Message: "
                        + connection.getResponseMessage());
            }

            br = new BufferedReader(new InputStreamReader(
                    (connection.getInputStream())));

            setMessages(br.readLine());

            connection.disconnect();

        } catch (Exception e) {
            logger.error("error: ", e);
        }
    }

    @Override
    public void put(String params) {
        try {
            url = new URL(uri);
            if (params != null) {
                url = new URL(uri + "/" + params);
            }
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Accept", "application/json");
            if (params != null) {
                os = connection.getOutputStream();
                os.write(params.getBytes());
                os.flush();
            }
            if (connection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : " + connection.getResponseCode() + "Message: "
                        + connection.getResponseMessage());
            }

            br = new BufferedReader(new InputStreamReader(
                    (connection.getInputStream())));

            setMessages(br.readLine());

            connection.disconnect();

        } catch (Exception e) {
            logger.error("error: ", e);
        }
    }

    @Override
    public void delete(String params) {
        try {
            url = new URL(uri);
            if (params != null) {
                url = new URL(uri + "/" + params);
            }
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("DELETE");
            connection.setRequestProperty("Accept", "application/json");
            if (connection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : " + connection.getResponseCode() + "Message: "
                        + connection.getResponseMessage());
            }

            br = new BufferedReader(new InputStreamReader(
                    (connection.getInputStream())));

            setMessages(br.readLine());

            connection.disconnect();

        } catch (Exception e) {
            logger.error("error: ", e);
        }
    }

    @Override
    public void get(String params) {
        try {
            url = new URL(uri);
            if (params != null) {
                url = new URL(uri + "/" + params);
            }
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("GET");

            connection.setRequestProperty("Accept", "application/json");
            if (connection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : " + connection.getResponseCode() + "Message: "
                        + connection.getResponseMessage());
            }

            br = new BufferedReader(new InputStreamReader(
                    (connection.getInputStream())));

            String a = "";
            while (true) {
                String line = br.readLine();
                if (line == null) {
                    break;
                }
                a += line;
            }
            setMessages(a);

            connection.disconnect();

        } catch (Exception e) {
            logger.error("error: ", e);
        }
    }
}
